﻿/**
* @description  Uses a pixel dissolve to reveal or remove an image.
*/
package com.foed.animation.transitions {

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import com.foed.animation.Animator;
	
	public class DissolveTransition extends Transition {
		
		private var _startImage:BitmapData;
		private var _destImage:BitmapData;
		private var _transitionBitmap:Bitmap;
		private var _dissolveImage:BitmapData;
		private var _dissolveVars:Object;
	
		/**
		* @description  Constructor.
		*
		* @param  pClip  The clip on which to perform the transition.
		* @param  pStartTransform  The starting values for the color transition.
		* @param  pEndTransform  The destination values for the color transition.
		* @param  pTime  The length in milliseconds for the transition.
		* @param  pType  The type of transition. IN moves from startTransform to endTransfom. OUT does the opposite.
		* @param  pEaseFunction  The function to use to transform the transition values over time.
		*/
		public function DissolveTransition(
			pClip:DisplayObject=null,
			pStartTransform:Object=null,
			pEndTransform:Object=null,
			pTime:Number=1000,
			pType:String=null,
			pEaseFunction:Function=null
		) {
			super(pClip, pTime, 0, 1, pType, pEaseFunction);
		}
	
		/**
		* @description  Called each iteration of the tween by the TweenRunner to transform values.
		*
		* @param  pChangeValues  The current values of the transition at each iteration of the tween.
		*/
		override protected function runTransition(pChangeValues:Object):void {
			_startImage.draw(_dissolveImage);
			_dissolveVars.seed = Number(
				_startImage.pixelDissolve(
					_destImage,
					_dissolveVars.rect,
					_dissolveVars.point,
					_dissolveVars.seed,
					_dissolveVars.pixels
				)
			);
			super.runTransition(pChangeValues);
		}
		
		/**
		* @description  Prepares clip to have transitions applied by adding new parent clip.
		*/
		override protected function prepare():void {
			super.prepare();
			var pWidth:Number = clip.width;
			var pHeight:Number = clip.height;
			_startImage = new BitmapData(pWidth, pHeight, true, 0x00FFFFFF);
			_destImage = new BitmapData(pWidth, pHeight, true, 0x00FFFFFF);
			if (type == TransitionType.IN) {
				_destImage.draw(clip);
			} else {
				_startImage.draw(clip);
			}
			clip.visible = false;
			_dissolveImage = new BitmapData(pWidth, pHeight, true, 0x00FFFFFF);
			_transitionBitmap = new Bitmap(_startImage);
			_clipHolder.addChild(_transitionBitmap);
		}
	
		/**
		* @description  Removes any artifacts of the transition.
		*/
		override protected function cleanUp():void {
			super.cleanUp();
			_startImage.dispose();
			_destImage.dispose();
		}

		/**
		* @description  Begins the transition.
		*/
		override public function start():void {
			var pWidth:Number = clip.width;
			var pHeight:Number = clip.height;
			_dissolveVars = {};
			_dissolveVars.pixels = (pWidth*pHeight)/(time/Animator.intervalTime);
			_dissolveVars.rect = new Rectangle(0, 0, pWidth, pHeight);
			_dissolveVars.point = new Point();
			_dissolveVars.seed = Number(new Date());
			super.start();
		}
		
	}
	
}